package qscript;

import java.awt.Color;

import processing.core.PApplet;
import g4p_controls.G4P;
import g4p_controls.GEditableTextControl;
import g4p_controls.GEvent;
import g4p_controls.GTextArea;

public class CodeEditorTest01 extends PApplet {

	GTextArea editor;

	String[] code;
	boolean changed;
	
	int lineNo = 8, charNo = 30;
	
	public void setup(){
		size(400,400);

		code = getCode(1);

		editor = new GTextArea(this,20,10,140,140,G4P.SCROLLBARS_BOTH);
		editor.setText(code, 2000);

		
		println(editor.getText());
		
	}
	
	public void mouseClicked(){
		editor.moveCaretTo(lineNo, charNo);
		editor.addStyle(G4P.BACKGROUND, Color.YELLOW, lineNo, 0, charNo);
	}

	public void draw(){
		background(200,200,255);
	}


	public void handleTextEvents(GEditableTextControl textcontrol, GEvent event) {
		if(textcontrol == editor){
			if(event == GEvent.CHANGED){
				editor.clearStyles();
				changed = true;	
			}
		}
	}

	public String[] getCode(int n){
		String[] lines ;
		switch(n){
		case 2:
			lines = new String[] { 
					"$n1 = 1; $n2 = 1; $nbrTerms = 12",
					"print($n1 + ' ' + $n2 + ' ')",
					"WHILE($nbrTerms > 2) ",
					"  $next = $n1 + $n2 ",
					"  print($next + ' ') ",
					"  $n1 = $n2; $n2 = $next",
					"  $nbrTerms = $nbrTerms - 1",
					"WEND",
					"println(''); END($next)"
			};
			break;
		case 1:
		default:
			lines = new String[] { 
					"$a=1;$b=-7;$c=-30" ,
					"$numer = $b*$b - 4*$a*$c",
					"IF($numer < 0)",
					"  println('Complex Roots')",
					"ELSE",
					"  $numer = sqrt($numer); $denom = 2*$a",
					"  $root1= (-1 * $b + $numer)/$denom",
					"  $root2= (-1 * $b - $numer)/$denom",
					"  println($root1 + '  &  ' + $root2);",
					"ENDIF"				
			};
			break;
		}
		return lines;
	}
}


/*
$a=1;$b=-7;$c=-30
$numer = $b*$b - 4*$a*$c
IF($numer < 0)
  println('Complex Roots')
ELSE
  $numer = sqrt($numer); $denom = 2*$a
  $root1= (-1 * $b + $numer)/$denom
  $root2= (-1 * $b - $numer)/$denom
  println($root1 + '  &  ' + $root2);
ENDIF
*/